#	$Id:	$

{

	package	bank;

	use strict "vars";
	use strict "subs";

	use lib $ENV{BANKS};

	use account;

	use Data::Dumper;

	our $number_of_banks = 0;    #	Example of class variable.

	sub new() {

		my $class = shift;

		my $name = shift;

		my $self = {
					 NAME     => $name,
					 ACCOUNTS => [],
					 INDEX    => undef,
					 };

		bless $self, $class;

		$number_of_banks++;

		print "This is bank number $number_of_banks\n";

		#	Add the accounts.  This would ordinarily come from the db.

		my @accounts;

		my $sg = {
				   NAME  => 'SG',
				   CLASS => 'currentAccount'
				   };
		push( @accounts, $sg );

		my $ak = {
				   NAME  => 'AK',
				   CLASS => 'savingsAccount'
				   };
		push( @accounts, $ak );

		print Dumper( \@accounts ) . "\n";

		foreach my $a ( @accounts ) {

			my $class = $a->{CLASS};

			require $class . ".pm";

			my $acct = $class->new( $a->{NAME} );

			$self->addAccount( $acct );
		}

		print Dumper( $self ) . "\n";

		return $self;
	}

	sub addAccount() {

		#	Add an account with the given characteristics to this bank.

		my $self    = shift;
		my $account = shift;

		push( @{ $self->{ACCOUNTS} }, $account );

		print Dumper( $self ) . "\n";
	}

	sub resetAccountIndex() {

		my $self = shift;

		$self->{INDEX} = undef;
	}

	sub setNextAccount() {

		#       Set the index of the next account

		my $self = shift;

		my $retval;

		if ( !defined $self->{INDEX} ) {
			$self->{INDEX} = 0;
		}
		else {
			$self->{INDEX} = $self->{INDEX} + 1;
		}

		if ( $self->getNextAccount ) {
			return 1;
		}
		else {
			return 0;
		}
	}

	sub getNextAccount() {

		my $self = shift;

		my $index = $self->{INDEX};

		return @{ $self->{ACCOUNTS} }[$index];

	}

	sub execute() {

		#	Run the bank by iterating thru all accounts

		my $self = shift;

		$self->resetAccountIndex;

		while ( $self->setNextAccount ) {

			my $acct = $self->getNextAccount;

			$acct->execute;

		}

	}

}    #	Class:	bank

1;

